package service;

import util.CheckParameters;
import dao.BalanceDAO;
import domain.Balance;
import exception.InvalidBusinessStateException;

public class BalanceService extends GenericService<Balance> {
	
	private static BalanceService instance;
	
	public static BalanceService getInstance() {
		if (instance == null)
			instance = new BalanceService();
		return instance;
	}

	public BalanceService() {
		dao = BalanceDAO.getInstance();
	}
	
	public Balance findByRoommate(Integer roommateId) {
		return ((BalanceDAO) dao).findByRoommate(roommateId);
	}
	
	@Override
	protected void businessRules(Balance balance)
			throws InvalidBusinessStateException {
		CheckParameters.checkNull(balance.getRoommate(), "Roommate");
	}

	@Override
	protected void handleDependencies(Balance entity)
			throws InvalidBusinessStateException {}

	@Override
	protected void cascadeDeletion(Integer id) throws InvalidBusinessStateException {
		if (find(id).getBalance() != 0.0) {
			throw new InvalidBusinessStateException("Balance cannot be deleted because it's not empty");
		}
	}

}
